package com.lxd.sunso.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.lxd.sunso.common.utils.PageUtils;
import com.lxd.sunso.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lxd.sunso.product.entity.DatatemplateEntity;
import com.lxd.sunso.product.service.DatatemplateService;


/**
 * @author lxd
 * @email sunlightcs@gmail.com
 * @date 2020-12-30 18:06:30
 */
@RestController
@RequestMapping("product/datatemplate")
public class DatatemplateController {
    @Autowired
    private DatatemplateService datatemplateService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:datatemplate:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = datatemplateService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("product:datatemplate:info")
    public R info(@PathVariable("id") Long id) {
        DatatemplateEntity datatemplate = datatemplateService.findById(id);

        return R.ok().put("datatemplate", datatemplate);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:datatemplate:save")
    public R save(@RequestBody DatatemplateEntity datatemplate) {
        boolean flag = datatemplateService.savedatatemplate(datatemplate);
        return flag ? R.ok() : R.error(1, "增加失败，当前数据模板下存在设备，请新建数据模板");
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:datatemplate:update")
    public R update(@RequestBody DatatemplateEntity datatemplate) {
        datatemplateService.newupdate(datatemplate);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        boolean flag = datatemplateService.removedatatemplate(Arrays.asList(ids));
        return flag ? R.ok() : R.error(1, "删除失败，当前数据模板下存在设备，请新建数据模板");
    }

    /**
     * 根据模板名获取
     */
    @RequestMapping("/search/{templateId}")
    public R search(@PathVariable("templateId") Long templateId) {
        List<DatatemplateEntity> entities = datatemplateService.search(templateId);
        return R.ok().put("datatemplate", entities);
    }

    /**
     * 根据设备名称获取历史数据的表头
     */
    @RequestMapping("/getHeadByMachineName/{machineName}")
    public List<String> getHeadByMachineName(@PathVariable String machineName) {
        List<String> data = datatemplateService.getHeadByMachineName(machineName);
        return data;
    }

}
